﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;

namespace Toolkit.Web.Infastructure
{
    public sealed class TextNode : IHtmlNode
    {
        private string _innerHtml;

        public TextNode(string value)
        {
            InnerHtml = value;
        }

        public string TagName
        {
            get
            {
                throw new NotSupportedException();
            }
        }

        public string InnerHtml
        {
            get
            {
                return _innerHtml;
            }
            set
            {
                _innerHtml = HttpUtility.HtmlEncode(value);
            }
        }

        public TagRenderMode RenderMode
        {
            get
            {
                throw new NotSupportedException();
            }
        }

        public IList<IHtmlNode> Children
        {
            get
            {
                throw new NotSupportedException();
            }
        }

        public IDictionary<string, string> Attributes
        {
            get
            {
                throw new NotSupportedException();
            }
        }

        public Action<IViewComponentWriter> Template
        {
            get
            {
                throw new NotSupportedException();
            }
            set
            {
                throw new NotSupportedException();
            }
        }

        public void ApplyTemplate(HtmlTemplate template)
        {
            throw new NotSupportedException();
        }

        public void ApplyTemplate<T>(HtmlTemplate<T> template, T dataItem) where T : class
        {
            throw new NotSupportedException();
        }

        public void AppendTo(IHtmlNode parent)
        {
            parent.Children.Add(this);
        }

        public void Append(IHtmlNode child)
        {
            Children.Add(child);
        }

        public void WriteTo(IViewComponentWriter writer)
        {
            writer.Write(InnerHtml);
        }

        public override string ToString()
        {
            using (var output = new StringWriter())
            {
                WriteTo(new ViewComponentWriter(output));
                return output.ToString();
            }
        }

        public string ToString(IFormatProvider format)
        {
            using (var output = new StringWriter(format))
            {
                WriteTo(new ViewComponentWriter(output));
                return output.ToString();
            }
        }

        public void AddClass(params string[] classes)
        {
            throw new NotSupportedException();
        }

        public void AddStyle(string key, string value)
        {
            throw new NotSupportedException();
        }

        public void MergeAttribute(string key, string value, bool replaceExisting)
        {
            throw new NotSupportedException();
        }

        public void MergeAttributes<TKey, TValue>(IDictionary<TKey, TValue> values, bool replaceExisting)
        {
            throw new NotSupportedException();
        }
    }
}
